package com.zp.resource.psychological;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.springframework.stereotype.Component;

import com.sun.jersey.spi.resource.Singleton;
import com.zp.bean.common.ResultCommonVo;
import com.zp.bean.psychological.ResultActivityVo;
import com.zp.domain.Activity;
import com.zp.resource.common.BaseServices;
import com.zp.util.Constant;
import com.zp.util.MyException;

/**
 * activity services
 * 
 * @author chenlijun08@gmail.com
 * 
 * @version 1.0
 * 
 * @date 2014/3/5
 * 
 */
@Component
@Path("/rest/activity")
@Singleton
public class ActivityServices extends BaseServices {

	/**
	 * add one Activity path:/activity/addActivity Http:POST
	 * 
	 * @param activity
	 * 
	 * @return Response, ResultCommonVo with JSON String
	 *         like:{"code":268,"message":"OK","totalCount":0}
	 */
	@POST
	@Path("/addActivity")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	public Response addActivity(Activity activity) {
		ResultCommonVo result = new ResultCommonVo();
		try {
			jpaDao.save(activity);
			result.setCode(Constant.SUCCESS_CODE);
			result.setMessage(Constant.OK);
		} catch (MyException e) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return Response.ok(result).build();
	}

	/**
	 * update one Activity path:/activity/updateActivity Http:PUT
	 * 
	 * @param activity
	 * 
	 * @return Response, ResultCommonVo with JSON String
	 *         like:{"code":268,"message":"OK","totalCount":0}
	 */
	@PUT
	@Path("/updateActivity")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	public Response updateActivity(Activity activity) {
		ResultCommonVo result = new ResultCommonVo();
		try {
			jpaDao.update(activity);
			result.setCode(Constant.SUCCESS_CODE);
			result.setMessage(Constant.OK);
		} catch (MyException e) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return Response.ok(result).build();
	}

	/**
	 * delete Activity by id path:/activity/deleteActivity Http:DELETE
	 * 
	 * @param activityId
	 *            primary key of Activity
	 * 
	 * @return Response, ResultCommonVo with JSON String
	 *         like:{"code":268,"message":"OK","totalCount":0}
	 */
	@DELETE
	@Path("/deleteActivity")
	@Produces(MediaType.APPLICATION_JSON)
	public Response deleteActivity(@QueryParam("activityId") String activityId) {
		logger.info("deleteActivity called activityId:" + activityId);
		ResultCommonVo result = new ResultCommonVo();
		try {
			jpaDao.deleteById(Activity.class, activityId);
			result.setCode(Constant.SUCCESS_CODE);
			result.setMessage(Constant.OK);
		} catch (MyException e) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return Response.ok(result).build();
	}

	/**
	 * 
	 * get activity for one organization path:/activity/getActivitys Http:POST
	 * 
	 * @param pageNo
	 * 
	 * @param pageSize
	 * 
	 * @param partyId
	 *            if ignore the default value will be null
	 * 
	 * @return Response, ResultActivityVo with JSON String
	 *         like:{"code":268,"message":"OK","totalCount":9,"list":[{"id":
	 *         "402880e44497794e0144977b65e80001"
	 *         ,"name":"社区宣传教育活动","partyId":"402880e44492d4ac014492d533850001"
	 *         ,"contentId"
	 *         :3},{"id":"402880e44497794e0144977c07930002","name":"关爱留守儿童活动"
	 *         ,"partyId":"402880e44492d4ac014492d533850001","contentId":3}]}
	 */
	@POST
	@Path("/getActivitys")
	@Produces({ MediaType.APPLICATION_JSON })
	public Response getActivityByOrgId(@QueryParam("pageNo") int pageNo,
			@QueryParam("pageSize") int pageSize,
			@QueryParam("partyId") String partyId) {
		logger.info("getActivityByOrgId called json pageNo:" + pageNo
				+ " pageSize:" + pageSize + " partyId:" + partyId);
		String jpql = "from Activity a,Content n where a.contentId=n.id and a.partyId=? order by n.operationTime desc";
		String countSql = "select count(id) from Activity where partyId=?";
		List<?> list = null;
		ResultActivityVo result = new ResultActivityVo();
		List<Activity> lt = new ArrayList<Activity>();
		result.setCode(Constant.SUCCESS_CODE);
		result.setMessage(Constant.OK);
		if ("".equals(partyId) || partyId == null) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(Constant.OBJECT_NOT_EXITS);
		} else {
			Object[] param = { partyId };
			try {
				list = this.jpaDao.getResultData(pageNo, pageSize, jpql, param);
				long totalCount = this.jpaDao.getCount(countSql, param);
				result.setTotalCount(totalCount);
			} catch (MyException e) {
				result.setCode(Constant.FAILE_CODE);
				result.setMessage(e.getMessage());
				e.printStackTrace();
			}
		}
		if (list != null && list.size() > Constant.ZERO) {
			for (Object o : list) {
				Object[] data = (Object[]) o;
				Activity activity = (Activity) data[Constant.ZERO];

				lt.add(activity);
			}
			result.setList(lt);

		}
		return Response.ok(result).build();
	}
}
